import os
from flask import Flask, Response, render_template_string   
import time
   
app = Flask(__name__)

   
@app.route('/sse')   
def sse():
    def event_stream():
        while True:
            current_time = time.strftime('%H:%M:%S')
            yield f"data: {current_time}\n\n"
            time.sleep(1)

    return Response(event_stream(), mimetype='text/event-stream')

   
@app.route('/')   
def index():
    return render_template_string('''
        <h1>SSE实时时钟</h1>
        <div id="clock"></div>
        <script>
            const clock = document.getElementById('clock');
            const sse = new EventSource('/sse');
            sse.onmessage = (event) => {
                clock.textContent = event.data;
            };
        </script>
    ''')

   
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 80)))